package com.wy.sloth.model;

import java.io.Serializable;

import com.alibaba.fastjson.JSONObject;
import com.wy.sloth.constant.SlothConstants;
import com.wy.sloth.util.StringUtils;

import lombok.Getter;
import lombok.Setter;

/**
 * 消息处理回执消息
 * 
 * @author wangyuan
 * @since 2019年3月18日
 */
@Getter
@Setter
public class SlothReceiptData implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -846795766640065419L;
    
    /**
     * 消息处理状态，处理成功为1，失败为0
     */
    private Integer status;
    
    /**
     * 消息处理结果之后反馈的数据，需要数据反馈才返回
     */
    private JSONObject receiptData;
    
    /**
     * 消息处理失败时的错误信息
     */
    private String errorMsg;
    
    /**
     * 回执对应的消息id
     */
    private String msgId;
    
    /**
     * 回执对应的处理队列的名称
     */
    private String queueName;

    public SlothReceiptData(Integer status, JSONObject receiptData, String errorMsg, String msgId, String queueName) {
        super();
        this.status = status;
        this.receiptData = receiptData;
        this.errorMsg = errorMsg;
        this.msgId = msgId;
        this.queueName = queueName;
    }

    public SlothReceiptData(Integer status, String msgId, String queueName) {
        this(status, null, null, msgId, queueName);
    }

    public SlothReceiptData(Integer status, JSONObject receiptData, String msgId, String queueName) {
        this(status, receiptData, null, msgId, queueName);
    }

    public SlothReceiptData(Integer status, String errorMsg, String msgId, String queueName) {
        this(status, null, errorMsg, msgId, queueName);
    }

    public SlothReceiptData(String msgId, String queueName) {
        this(null, null, null, msgId, queueName);
    }
    
    public SlothReceiptData() {
        super();
    }
    
    public String getHashKey() {
        return msgId + SlothConstants.REDIS_NAMESPACE_SEPARATOR + queueName;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("消息（id=" + msgId + "）");
        sb.append("处理结果：" + (Integer.valueOf(SlothReceipt.STATUS_SUCCESS).equals(status) ? "成功" : "失败") + "；");
        sb.append("回执数据：" + (receiptData == null ? "" : receiptData.toJSONString()) + "；");
        sb.append("回执错误信息：" + (StringUtils.isEmpty(errorMsg) ? "" : errorMsg) + "；");
        sb.append("处理队列：" + queueName + "；");
        return sb.toString();
    }
    
    public JSONObject toJsonObject(){
        return (JSONObject) JSONObject.toJSON(this);
    }
    
}
